#include "pch.h"
#include "GridCellsHolder.h"

using namespace TicTacToe;

GridCellsHolder::GridCellsHolder(void)
{
	m_columns = ref new Platform::Collections::Vector<Column^>();
}
/*
GridCellsHolder::~GridCellsHolder(void)
{
}*/

WFC::IVector<Column^>^ GridCellsHolder::Columns::get (void) { 
	return m_columns;
}

void GridCellsHolder::SetCurrentPlayerInCell(int column, int row, int player){
	m_columns->GetAt(column)->Cells->GetAt(row)->Player = player;
}

int GridCellsHolder::GetCurrentPlayerInCell(int column, int row){
	if (m_columns->Size > column && m_columns->GetAt(column)->Cells->Size > row){
		return m_columns->GetAt(column)->Cells->GetAt(row)->Player;
	} else
		return -1;
}

WFC::IVector<Cell^>^ GridCellsHolder::GetFreeCells()
{
	Platform::Collections::Vector<Cell^>^ freeCells = ref new Platform::Collections::Vector<Cell^>();

	for (int i=0;i < m_columns->Size; i++){
		for (int j =0; j < m_columns->GetAt(i)->Cells->Size; j++){
			if(m_columns->GetAt(i)->Cells->GetAt(j)->Player <= 0){
				freeCells->Append(m_columns->GetAt(i)->Cells->GetAt(j));
			}
		}
	}

	return (WFC::IVector<Cell^>^)freeCells;
}

void GridCellsHolder::SetDimension(int column, int row){
	for (int i = 0; i < column; i++)
	{
		Column^ col = ref new Column();
		for (int j = 0; j < row; j++)
		{
			Cell^ cel = ref new Cell(i, j);
			col->Cells->Append(cel);
		}
		m_columns->Append(col);
	}
}
